﻿@using System.Web.Optimization
@using VocaDb.Model.Domain
@using VocaDb.Model.Helpers
@using VocaDb.Web.Helpers;
@using ValidationErrors = VocaDb.Model.Resources.ArtistValidationErrors;
@using Res = ViewRes.Artist.EditStrings;
@using EditRes = ViewRes.EntryEditStrings;
@inherits VocaDb.Web.Code.VocaDbPage<VocaDb.Web.Models.Artist.ArtistEditViewModel>

@{
	PageProperties.Title = string.Format(Res.EditTitle, Model.Artist.Name);
	ViewBag.Parents = new[] { 
		Html.ActionLink(ViewRes.SharedStrings.Artists, "Index", "Search", UrlMapper.Search.Artists(), null), 
		Html.ActionLink(Model.Artist.Name, "Details", new { id = Model.Artist.Id }) 
	};

	var validationErrors = new Dictionary<string, string> {
		{ "validationError_unnecessaryPName", ValidationErrors.UnnecessaryPName },
		{ "validationError_unspecifiedNames", ValidationErrors.UnspecifiedNames },
		{ "validationError_needReferences", ValidationErrors.NeedReferences },
		{ "validationError_needType", ValidationErrors.NeedType },
	};
	
}

@section Toolbar {
@if (Model.CanDelete) {
	if (!Model.Artist.Deleted) {
		<a href="#" id="deleteLink" data-bind="click: function() { deleteViewModel.dialogVisible(true); }">@ViewRes.SharedStrings.Delete</a>
	} else if (Login.CanDeleteEntries) {
		@Html.ActionLink(EditRes.Restore, "Restore", new { id = Model.Artist.Id }, new { id = "restoreLink" })		
	}
}
@if (Login.CanMergeEntries) {
	@Html.Raw("&nbsp;")	
	@Html.ActionLink(EditRes.Merge, "Merge", new { id = Model.Artist.Id }, new { id = "mergeLink" })
}
}

@Helpers.ValidationSymmaryPanel("Unable to save")

@KnockoutHelpers.EntryValidationMessage(Model.Artist.Status == EntryStatus.Draft, validationErrors, "glartists")

<br />
<form action="@Url.Action("Edit", "Artist")" method="POST" enctype="multipart/form-data" data-bind="submit: submit, visible: true" style="display: none;">

	@Helpers.SaveAndBackBtn(Url.Action("Details", new { Model.Artist.Id }))
	
	<div id="tabs">

		<ul>
			<li>
				<a href="#basicInfoTab">@EditRes.BasicInfo</a>
			</li>
			<li>
				<a href="#picsTab">@Res.TabAdditionalPictures</a>
			</li>
		</ul>

		<div id="basicInfoTab">
			<div>
				<div class="editor-label">
					<label>@EditRes.DefaultLanguageSelection</label>
				</div>
				<div class="editor-field">
					@KnockoutHelpers.LanguageSelectionDropDown("defaultNameLanguage")
				</div>

				<div class="editor-label">
					@Helpers.HelpLabel(Res.BaNames, ViewRes.EntryEditStrings.NameHelp)
					@Helpers.RequiredField()
					<span data-bind="visible: validationError_unspecifiedNames, qTip" class="icon errorIcon" title="@ValidationErrors.UnspecifiedNames"></span>
				</div>
				<div class="editor-field" data-bind="with: names">
					@{ Html.RenderPartial("KnockoutPartials/_NamesEditor", new VocaDb.Web.Models.Shared.NamesEditorViewModel()); }
				</div>

				<div class="editor-label">
					<label>@Res.BaMainPicture</label>
				</div>
				<div class="editor-field">
					<table>
						<tr>
							<td>
								<img src="@Url.Action("PictureThumb", new { id = Model.Artist.Id })" alt="Artist picture" class="coverPic" />					
							</td><td>
								<p>@string.Format(Res.BaPictureInfo, string.Join(", ", ImageHelper.AllowedExtensions), ImageHelper.MaxImageSizeMB)</p>
								<input type="file" id="pictureUpload" name="pictureUpload" />					
							</td>
						</tr>
					</table>
				</div>

				<div class="editor-label">
					<label class="inline-block">@Res.BaDescription</label>
					@Helpers.MarkdownNotice()
					<span data-bind="visible: validationError_needReferences, qTip" class="icon errorIcon" title="@ValidationErrors.NeedReferences"></span>
				</div>
				<div class="editor-field entry-edit-description" data-bind="with: description">
					@{ Html.RenderPartial("Partials/_EnglishTranslatedStringEdit"); }
				</div>

				<div class="editor-label">
					<label>@Res.BaArtistType</label>
				</div>
				<div class="editor-field">
					@KnockoutHelpers.DropdownList(Model.AllArtistTypes, "artistTypeStr")
					<span data-bind="visible: validationError_needType, qTip" class="icon errorIcon" title="@ValidationErrors.NeedType"></span>
				</div>
				
				<!-- ko if: canHaveRelatedArtists -->
					<div class="editor-label">
						@Helpers.HelpLabel("Associated artists", "Artists related to this voicebank. These are inherited to derived voicebanks. " +
							"Character designer only needs to be specified if it's different from the illustrator and manager only needs to be specified if it's different from the voice provider.")
					</div>
					<div class="editor-field">
						<table>
							<tr>
								<th>Role</th>
								<th>Artist</th>
								<th></th>
							</tr>
							<tr>
								<td>
									@Helpers.HelpLabel("Illustrator", "Person who illustrated the character design. This is inherited to derived voicebanks.")
								</td>
								<td>
									@KnockoutHelpers.ArtistLockingAutoComplete("illustrator", "Unknown,Circle,Producer,Illustrator,Animator,Lyricist,OtherVocalist,OtherGroup,OtherIndividual")									
								</td>
								<td></td>
							</tr>
							<tr>
								<td>
									@Helpers.HelpLabel("Voice provider", "Person who provided their voice for this voicebank. This is inherited to derived voicebanks.")
								</td>
								<td>
									@KnockoutHelpers.ArtistLockingAutoComplete("voiceProvider", "Unknown,Producer,Illustrator,Animator,Lyricist,OtherVocalist,OtherIndividual")									
								</td>
							</tr>
							<!-- ko foreach: associatedArtists -->
								<tr>
									<td>
										@KnockoutHelpers.DropdownList(Model.AssociatedArtistTypes, "linkType")
									</td>
									<td>
										<div class="input-append">
											<input type="text" readonly data-bind="value: parent.name" />
											<button class="btn btn-danger" data-bind="click: function() { $parent.associatedArtists.remove($data); }">Remove</button>											
										</div>
									</td>
								</tr>
							<!-- /ko -->
							<tr>
								<td>
									@KnockoutHelpers.DropdownList(Model.AssociatedArtistTypes, "newAssociatedArtistType")
								</td>
								<td>
									@KnockoutHelpers.ArtistLockingAutoComplete("newAssociatedArtist", "Unknown,Circle,Producer,Illustrator,Animator,Lyricist,OtherVocalist,OtherGroup,OtherIndividual")									
								</td>
							</tr>
						</table>
					</div>				
				<!-- /ko -->

				<!-- ko if: canHaveCircles -->
					<div class="editor-label">
						<label>@Res.BaGroups</label>
					</div>
					<div class="editor-field">
						@* TODO: extract template and viewmodel *@
						<table>
							<tbody data-bind="foreach: groups">
								<tr>
									<td>
										<a data-bind="attr: { href: vdb.utils.EntryUrlMapper.details('Artist', parent.id) }, text: parent.name, artistToolTip: parent.id"></a>
									</td>
									<td>
										<a data-bind="click: $parent.removeGroup" href="#" class="textLink removeLink">@ViewRes.SharedStrings.Remove</a>
									</td>
								</tr>
							</tbody>
						</table>
						
						<input type="text" data-bind="artistAutoComplete: groupSearchParams" maxlength="128" placeholder="@ViewRes.SharedStrings.Search" class="input-xlarge" />
					</div>
				<!-- /ko -->

				<!-- ko if: allowBaseVoicebank -->			
				<div class="editor-label">
					<label>
						@Res.BaBaseVoicebank
					</label>
				</div>
				<div class="editor-field">
					<div style="display: inline-block;" class="input-append">
						@KnockoutHelpers.LockingAutoComplete("artistAutoComplete", "baseVoicebankSearchParams", textBinding: "baseVoicebank.name", valBinding: "baseVoicebank.id")
					</div>
				</div>		
				<!-- /ko -->		
				
				<!-- ko if: canHaveReleaseDate -->	
				<div class="editor-label">
					<label>
						@Res.BaReleaseDate
					</label>
				</div>
				<div class="editor-field">
					<input type="text" data-bind="datepicker: { value: releaseDate, dateFormat: 'yy-mm-dd' }" maxlength="10" />
				</div>
				<!-- /ko -->		

				<div class="editor-label">
					@Helpers.HelpLabel(EditRes.WebLinks, EditRes.ExternalLinksQuickHelp)
				</div>
				<div class="editor-field" data-bind="with: webLinks">
					@KnockoutHelpers.WebLinksEditViewKnockout(true)
				</div>

				<div class="editor-label">
					@Helpers.HelpLabel(EditRes.Status, Resources.CommonMessages.EntryStatusExplanation)
				</div>
				<div class="editor-field">
					@KnockoutHelpers.DropdownList(Translate.EntryStatusNames.GetValuesAndNamesStrings(Model.AllowedEntryStatuses), "status")
				</div>

			</div>
		</div>
		
		<div id="picsTab" data-bind="with: pictures">
			
			<p>@Res.PiPicturesNote</p>
			<p>@string.Format(Res.BaPictureInfo, string.Join(", ", ImageHelper.AllowedExtensions), ImageHelper.MaxImageSizeMB)</p>

			<table>
				<tbody data-bind="foreach: pictures">
					@{ Html.RenderPartial("KnockoutPartials/_EntryPictureFileEdit"); }
				</tbody>
			</table>
			
			<a data-bind="click: add" href="#" class="addLink textLink">@Res.PiAdd</a>

		</div>

	</div>
	<br />
	
	<p>@EditRes.UpdateNotes</p>
	<textarea data-bind="value: updateNotes" class="input-xxlarge" rows="4" maxlength="200"></textarea>
	
	<br />	
	@Helpers.SaveAndBackBtn(Url.Action("Details", new { Model.Artist.Id }))
	
	<!-- JSON model to be posted to the server -->
	@Html.HiddenForKnockout(m => m.EditedArtist, "value: submittedJson")

</form>

@EntryDetailsHelpers.EntryDeletePopup(ViewRes.EntryEditStrings.ConfirmDelete)

@section BodyScripts {
@Scripts.Render("~/bundles/shared/edit", "~/bundles/Artist/Edit")

<script type="text/javascript">

	$(document).ready(function() {

		vdb.resources.entryEdit = {
			saveWarning: @ToJS(EditRes.SaveWarning)
		};

		var urlMapper = new vdb.UrlMapper('@RootPath');
		var repoFactory = new vdb.repositories.RepositoryFactory(urlMapper, @LanguagePreferenceInt);
		var artistRepo = repoFactory.artistRepository();
		var userRepo = repoFactory.userRepository();
		var editedModel = @Html.Raw(JsonHelpers.Serialize(Model.EditedArtist));
		var webLinkCategoryJson = @Html.Raw(JsonHelpers.Serialize(Translate.WebLinkCategoryNames.AllFields));

		if (editedModel) {
			ko.applyBindings(new vdb.viewModels.ArtistEditViewModel(artistRepo, userRepo, urlMapper, webLinkCategoryJson, editedModel, new vdb.ui_dialog.DialogService()));
		} else {

			artistRepo.getForEdit(@Model.Artist.Id, function (model) {
				ko.applyBindings(new vdb.viewModels.ArtistEditViewModel(artistRepo, userRepo, urlMapper, webLinkCategoryJson, model, new vdb.ui_dialog.DialogService()));
			});

		}

		initPage();

	});

</script>
}